package com.zjh.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class MyGlobalFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("自定义全局过滤器");
        //获取header 签名
        ServerHttpRequest request = exchange.getRequest();
        HttpHeaders headers = request.getHeaders();
        String s = headers.get("user-info").get(0);
        // Set<Map.Entry<String, List<String>>> entries = headers.entrySet();
       // System.out.println("header-list:"+entries);
        //进行
        ServerWebExchange serverWebExchange = exchange.mutate().request(builder -> {
            builder.header("user-info", s);
        }).build();
        return chain.filter(serverWebExchange);
    }
}
